<?php
class ModelCommunityLottery extends Model {
    public function editLottery($data){
        $lottery_data=array();

        foreach($data['lottery'] as $key => $value){
            $lottery_data[$key]['id']=$value['id'];
            $lottery_data[$key]['prize']=trim($value['prize']);
            $lottery_data[$key]['item']=trim($value['item']);
            $lottery_data[$key]['v']=trim($value['v']);
        }

        $this->db->query("DELETE FROM " . DB_PREFIX . "setting WHERE store_id = '0' AND `key` = 'lottery'");

        $this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '0', `key` = 'lottery', `group`='community_contests', `value`='" . $this->db->escape(serialize($lottery_data)) . "', `serialized`='1'");

        $this->sql_cache->delete('lottery');
    }

    public function getLottery(){
        $data=$lottery_array=$this->config->get('lottery');

        foreach($lottery_array as $key => $lottery){
            if($key<3){
                $query=$this->db->query("SELECT name FROM " . DB_PREFIX . "product_description WHERE product_id='" . $lottery['prize'] . "' AND language_id='" . (int)$this->config->get('config_language_id') . "'");

                $data[$key]['item']= $query->row['name'];
            }else{
                $data[$key]['item']= $lottery['item'];
            }
        }

        return $data;
    }
}
?>